<!--
 * @Description:
 * @Author: ZachGmy
 * @Date: 2022-06-28 15:34:58
 * @LastEditors: ZachGmy
 * @LastEditTime: 2022-11-27 20:06:54
-->
<template>
  <div>
    <el-row class="btns">
      <el-button type="primary" @click="gotoInputPage">上报</el-button>
    </el-row>
    <el-table size="small" :data="data.list">
      <el-table-column label="序号" prop="id"></el-table-column>
      <el-table-column
        label="国"
        prop="country"
        width="55"
        min-width="150"
      ></el-table-column>
      <el-table-column label="省" prop="province"></el-table-column>
      <el-table-column label="市" prop="city"></el-table-column>
      <el-table-column label="区" prop="area" min-width="200"></el-table-column>
      <el-table-column
        label="新增确诊"
        prop="confirmed"
        min-width="75"
        align="center"
        header-align="center"
      >
        <template slot-scope="scope">
          <span>
            {{ scope.row.confirmed }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="疑似"
        prop="suspected"
        min-width="50"
        align="center"
        header-align="center"
      >
        <template slot-scope="scope">
          <el-tag type="warning" size="small">{{ scope.row.suspected }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="死亡"
        prop="dead"
        min-width="50"
        align="center"
        header-align="center"
      >
        <template slot-scope="scope">
          <el-tag type="info" size="small">{{ scope.row.dead }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="治愈"
        prop="cure"
        min-width="50"
        align="center"
        header-align="center"
      >
        <template slot-scope="scope">
          <el-tag type="success" size="small">{{ scope.row.cure }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="重症"
        prop="severe"
        min-width="50"
        align="center"
        header-align="center"
      >
        <template slot-scope="scope">
          <el-tag type="danger" size="small">{{ scope.row.severe }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="境外输入"
        prop="outside"
        min-width="75"
        align="center"
        header-align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.outside }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="上报时间"
        prop="inputTime"
        min-width="110"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.inputTime | fmtDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="medium"
            style="color: #409eff"
            @click="openEditDialog(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="medium"
            style="color: red"
            @click="deleteCategory(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      :current-page="param.page"
      :page-size="param.pageSize"
      :total="data.total"
      @current-change="currentChangeHandler"
    >
    </el-pagination>
    <el-dialog :title="`修改 #${editingID}`" :visible.sync="editingDialog">
      <RecordForm
        :id="editingID"
        :data="row"
        @on-submit-success="onSubmitSuccess"
      />
    </el-dialog>
  </div>
</template>

<script>
import { deleteById, pageQuery } from '@/api/epidemic'
import RecordForm from './components/RecordForm.vue'
export default {
  components: {
    RecordForm
  },
  data() {
    return {
      param: {
        page: 1,
        pageSize: 15
      },
      data: {
        total: 0,
        list: []
      },
      editingID: 0,
      editingDialog: false,
      row: {}
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      pageQuery(this.param.page, this.param.pageSize).then((res) => {
        this.data = res.data
      })
    },
    currentChangeHandler(page) {
      this.param.page = page
      this.loadData()
    },
    gotoInputPage() {
      this.$router.push('/data/input')
    },
    onSubmitSuccess() {
      this.editingDialog = false
      this.loadData()
    },
    openEditDialog(row) {
      console.log(row.id)
      this.editingDialog = true
      this.editingID = row.id
      this.row = {
        id: row.id,
        country: row.country,
        province: row.province,
        city: row.city,
        area: row.area,
        confirmed: row.confirmed,
        suspected: row.suspected,
        dead: row.dead,
        cure: row.cure,
        severe: row.severe,
        outside: row.outside,
        inputTime: row.inputTime
      }
      // console.log(this.row)
    },
    async deleteCategory(id) {
      if (!id) {
        this.$message.error('错误：此行数据不存在 id')
        return
      }
      try {
        await this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteById(id)
        this.loadData() // 刷新数据
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      } catch (e) {
        if (e !== 'cancel' || e !== 'close') {
          this.$message.error('错误：' + e)
        }
      }
    }
  }
}
</script>

<style scoped></style>
